package com.itheima.goods.service.impl;

import com.itheima.goods.dao.BrandMapper;
import com.itheima.goods.pojo.Brand;
import com.itheima.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;

    //查询全部列表数据
    public List<Brand> findAll(){
        return brandMapper.selectAll();
    }

    @Override
    //根据ID查询品牌
    public Brand findById(Integer id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    @Override
    //新增品牌
    public void add(Brand brand) {
        brandMapper.insert(brand);
    }

    @Override
    //修改品牌
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKey(brand);

    }

    @Override
    //删除品牌
    public void delete(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    @Override
    //多条件搜索品牌方法 **** 明天继续看
    public List<Brand> findList(Map searchMap) {
        Example example=new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 品牌名称
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }
            // 品牌的首字母
            if(searchMap.get("letter")!=null && !"".equals(searchMap.get("letter"))){
                criteria.andEqualTo("letter",searchMap.get("letter"));
            }
        }
        return brandMapper.selectByExample(example);
    }


}
